import { _decorator, Color, Component, Label, Node } from 'cc';
import {  Building } from '../entity/Buildings';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { BuildingDetail } from './BuildingDetail';
import { BuildingUpGrade } from './BuildingUpGrade';
import { UserInfo } from '../common/UserInfo';
import { BManager } from '../entity/BManager';
import { Tech } from '../entity/Tech';
const { ccclass, property } = _decorator;

@ccclass('BuildingCon')
export class BuildingCon extends Component {
    @property(Label)
    buildingName:Label
    @property(Node)
    buildBtn:Node
    @property(Node)
    upGrade:Node

    private data:any
    start() {
        this.buildBtn.on(Node.EventType.TOUCH_END, this.onBuild, this)
        this.upGrade.on(Node.EventType.TOUCH_END, this.onUpGrade, this)
    }

    setData(data:{
        id: number,
        name: string,
        level: number,
        isLack?: boolean,
        isExit?: boolean,
        type?:string
    }){
        
         this.data = data
        this.buildingName.string = `${data.name}  等级${data.level}`
        
        if(data.isLack) {
            this.buildingName.color = Color.RED
            if(data.isExit){
                this.buildBtn.active = false
                this.upGrade.active = true
                this.upGrade.position = this.buildBtn.position
            } else {
                this.buildBtn.active = true
                this.upGrade.active = false
            }
        } else {
            this.buildBtn.active = false
            this.upGrade.active = false
        }
        
    }

    async onBuild(){
        if(this.data.type == 'tech'){
            UIManager.instance.clear()
            UIManager.instance.openUI(PrefabEnum.DevelopIndex)
            return
        }

        let building = Building.createByIdAndLevel(this.data.id, this.data.level)
        
        let ui = await UIManager.instance.openUI(PrefabEnum.BuildingDetail)
        ui.getComponent(BuildingDetail).setData(building)

        UIManager.instance.closeUI(PrefabEnum.BuildingDetail)
    }

    async onUpGrade(){
        if(this.data.type == 'tech'){
            UIManager.instance.clear()
            UIManager.instance.openUI(PrefabEnum.DevelopIndex)
            return
        }
        
        
        let ui = await UIManager.instance.openUI(PrefabEnum.BuildingUpGrade)
        
        let bm = BManager.getBManageById(this.data.id)
        let bui = Building.createBuildingByBManager(bm)
        ui.getComponent(BuildingUpGrade).setData(bui)

        UIManager.instance.closeUI(PrefabEnum.BuildingDetail)
    }
   
}


